package com.hinnotek.warehouse.viewmodel

import androidx.lifecycle.Transformations
import com.hinnotek.warehouse.api.WarehouseApi
import com.hinnotek.warehouse.utils.StringUtils
import com.lee.common.network.getRequestBody
import com.lee.common.utils.PAGE_SIZE
import com.lee.common.utils.SharedPreUtils
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

/**
 * 台账相关
 */
@HiltViewModel
class MachineAccountViewModel @Inject constructor() :
    BaseViewModel() {

    var applyNum = ""
    var applyType = ""
    var applyId = 0
    var applyDeviceId = 0

    var deviceName = ""
    var deviceModel = ""

    fun setParameters(applyNum: String, applyType: String) {
        this.applyNum = applyNum
        this.applyType = StringUtils.getApplyType(applyType)
    }

    //1
    private val machineAccount = Transformations.switchMap(page) {
        val map = mutableMapOf<String, Any>().apply {
            put("approvalUserId", SharedPreUtils.getInteger("userId",0))
            put("pageNo", it)
            put("pageSize", pageSize)
            //搜索
            if (applyNum != "") {
                put("applyNum", applyNum)
            }
            if (applyType != "") {
                put("applyType", applyType)
            }
        }
        WarehouseApi.get().getWarehourseApplyFormPage(getRequestBody(map))
    }

    //2
    private val machineAccountList = Transformations.switchMap(page) {
        WarehouseApi.get().getWarehourseApplyFormDeviceListByApplyId(applyId)
    }

    //详情
    private val machineAccountDetail = Transformations.switchMap(page) {
        WarehouseApi.get().getWarehourseApplyFormDetailListByApplyDeviceId(applyDeviceId)
    }

    //供应商
    private val deviceInfo = Transformations.switchMap(page) {
        val map = mutableMapOf<String, Any>().apply {
            put("inputOutput", "00")
            put("pageNo", it)
            put("pageSize", PAGE_SIZE)
            if (deviceName != "") {
                put("deviceName", deviceName)
            }
            if (deviceModel != "") {
                put("deviceModel", deviceModel)
            }
        }
        WarehouseApi.get().getDeviceAccountPage(getRequestBody(map))
    }

    val machineAccountPage = mapPage(machineAccount)

    val machineAccountListPage = mapPage(machineAccountList)

    val machineAccountDetailPage = mapPage(machineAccountDetail)

    val deviceInfoPage = mapPage(deviceInfo)

}